<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>学习主题</span>
      </div>
    </template>
    
    <div class="search-form">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="formInline.name" placeholder="请输入文件名称" clearable @keyup.enter="onSubmit"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="formInline.subjectimportant" placeholder="请输入学习重点" clearable @keyup.enter="onSubmit"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">
            <el-icon>
              <Search />
            </el-icon>
          </el-button>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="addSubject">新建</el-button>
      </div>
    </div>

    <div class="table-data" v-loading="loading">
      <el-table
        :data="subjectData"
        stripe
        borderresizable
        highlight-hover-row
        show-overflow
        keep-source
        scrollbar-always-on
      >
        <el-table-column
          v-for="(tableItem, tableIndex) in subjectTableColumns" :key="tableIndex"
          :prop="tableItem.field"
          :label="tableItem.label"
          :min-width="tableItem.minWidth"
          :formatter="tableItem.formatter"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          fixed="right"
          field="opera"
          label="操作"
          min-width="320"
        >
          <template #default="{ row }">
            <el-button
              type="warning"
              link
              @click="subjectDetail(row)"
            >查看</el-button>
            <el-button
              type="success"
              link
              @click="addRemind(row)"
            >新增学习提醒</el-button>
            <el-button
              type="success"
              link
              @click="addFilling(row)"
              v-if="isFilling"
            >新增学习填报</el-button>
            <el-button
              type="success"
              link
              @click="subjectEdit(row)"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="table-page">
        <el-pagination background layout="prev, pager, next" :total="dataTotal" :page-size="pageSize" :current-page="currentPage" @current-change="currentChange" />
      </div>
    </div>
  </el-card>
</template>

<script>
export default { name: "backendLearnSubject" }
</script>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router'
import { Search } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { http } from '@/utils/http/index';
import { dateFormatter, checkStatusFormatter } from "@/utils/backend.js"
import { useAuthStore } from '@/stores/modules/auth.js';

/* 路由 */
const route = useRouter();

/* 用户信息 */
const authStore = useAuthStore()

/* 当前用户是否具备学习上报权限 */
const isFilling = computed(() => { 
  let actions = authStore.userInfo.actions
  if(!actions) return
  const find = actions.find(item => item.id == '2.1')
  return find ? true : false
})

onMounted(() => {
  onSubmit()
})

/* 搜索表单 */
const formInline = ref({
  name: "",
  subjectimportant: "",
})

/* 分页 */
const dataTotal = ref(0)
const pageSize = ref(20)
const currentPage = ref(1)

if(route.currentRoute.value.query.currentPage) {
  currentPage.value = Number(route.currentRoute.value.query.currentPage)
}

/* 切换页码方法 */
const currentChange = (val) => {
  currentPage.value = val
  onSubmit()
}

/* 表格数据加载动画 */
const loading = ref(false)

/* 表格数据 */
const subjectData = ref([])

/* 表格字段 */
const subjectTableColumns = reactive([
  { field: "name", label: "文件名称", minWidth: "250" },
  { field: "subjectDesp", label: "简介", minWidth: "250" },
  { field: "subjectimportant", label: "学习重点", minWidth: "250" },
  { field: "studyWay", label: "学习形式", minWidth: "250" },
  { field: "studyStartDate", label: "学习计划开始日期", minWidth: "160", formatter: dateFormatter },
  { field: "studyEndDate", label: "学习计划结束日期", minWidth: "160", formatter: dateFormatter },
  { field: "studyRange", label: "学习范围", minWidth: "200" },
  { field: "subjectRequirement", label: "工作要求", minWidth: "200" },
  { field: "pubDate", label: "发布日期", minWidth: "160", formatter: dateFormatter },
  /* { field: "checkStatus", label: "审核状态", minWidth: "120", formatter: checkStatusFormatter }, */
])

/* 列表筛选 */
const onSubmit = () => {
  loading.value = true
  let params = {
    pageIndex: currentPage.value,
    pageSize: pageSize.value,
  }
  let dfi = {
    Logic: 0,
    Filters: []
  }

  for (let k in formInline.value){
    if(formInline.value[k]){
      dfi.Filters.push({
        Field: k,
        Operator: 0,
        value: formInline.value[k]
      })
    }
  }

  if(dfi.Filters.length){
    params.dfi = dfi
  }
  http.$POST("/TheoryStudyM/GetTheoryStudySubjectLocalInfoList", params).then(res => {
    loading.value = false
    if (res.isError) return
    subjectData.value = res.result.dataLst
    dataTotal.value = res.result.countAll
  }).catch(err => {
    loading.value = false
    ElMessage.warning("查询失败，请重试")
  })
}

/* 点击新建 */
const addSubject = () => {
  route.push({
    path: "/backend/learn-subject/add",
    query: { type: 'add' }
  })
}

/* 点击编辑 */
const subjectEdit = (row) => {
  route.push({
    path: "/backend/learn-subject/add",
    query: { id: row.id, type: 'edit', currentPage: currentPage.value }
  })
}

/* 点击查看详情 */
const subjectDetail = (row) => {
  let { href } = route.resolve({
    path: "/backend/learn-subject/detail",
    query: { id: row.id, currentPage: currentPage.value }
  })
  window.open(href, "_blank")
}

/* 点击新建学习提醒 */
const addRemind = (row) => {
  route.push({
    path: "/backend/learn-remind/add",
    query: { type: 'add', subjectId: row.id }
  })
}

/* 点击新建学习填报 */
const addFilling = (row) => {
  route.push({
    path: "/backend/learn-filling/add",
    query: { type: 'add', subjectId: row.id }
  })
}
</script>

<style lang='scss' scoped>
@import "@/assets/styles/backendElement.scss";
</style>